home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / SNIP9_91.ARJ / STATS.C < prev    next >
C/C++ Source or Header  |  1991-09-06  |  2KB  |  52 lines

  1. /****************************************************************/
  2. /*                                                              */
  3. /*      Collect file statistics                                 */
  4. /*                                                              */
  5. /*      Public domain demo program for analyzing encrypted      */
  6. /*      files. By: Bob Stout                                    */
  7. /*                                                              */
  8. /****************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include <math.h>
  12. #include <assert.h>
  13.  
  14. main(int argc, char *argv[])
  15. {
  16.         int i, ch, hist = 0;
  17.         long n = 0L;
  18.         double mean = 0., stdev = 0., ftmp;
  19.         static unsigned bins[256];
  20.         FILE *infile;
  21.  
  22.         assert(infile = fopen(argv[1], "rb"));
  23.         while (!feof(infile))
  24.         {
  25.                 if (EOF == (ch = fgetc(infile)))
  26.                         break;
  27.                 bins[ch] += 1;
  28.                 ++n;
  29.         }
  30.         fclose(infile);
  31.         for (i = 0; i < 256; ++i)
  32.         {
  33.                 mean += (double)(bins[i]);
  34.                 if (bins[i])
  35.                         ++hist;
  36.         }
  37.         mean /= 256.;
  38.         for (i = 0; i < 256; ++i)
  39.         {
  40.                 ftmp = (double)(bins[i]) - mean;
  41.                 stdev += (ftmp * ftmp);
  42.         }
  43.         ftmp  = stdev / 255.;
  44.         stdev = sqrt(ftmp);
  45.         printf("%ld Characters were read from %s\n"
  46.                 "There are an average of %f occurances of each character\n"
  47.                 "%d Characters out of 256 possible were used\n"
  48.                 "The standard deviation is %f\n"
  49.                 "The coefficient of variation is %f%%\n",
  50.                 n, argv[1], mean, hist, stdev, (100. * stdev) / mean);
  51. }
  52.